/**
 * @file test_basic_functionality.cpp
 * @brief 测试并发内存池基本功能
 * @description 测试ThreadCache、CentralCache和基本内存分配功能
 */

/**
 * @file test_basic_functionality.cpp
 * @brief 测试并发内存池基本功能
 */

#include "ConcurrentMemoryPool/ConcurrentAlloc.h"
#include "ConcurrentMemoryPool/Common.h"
#include <iostream>
#include <vector>
#include <thread>
#include <chrono>
#include <cassert>

// 测试基础分配和释放
void TestBasicAllocation()
{
    std::cout << "=== 测试基础分配和释放 ===" << std::endl;

    void *ptr1 = ConcurrentAlloc(64);
    void *ptr2 = ConcurrentAlloc(128);
    void *ptr3 = ConcurrentAlloc(256);

    std::cout << "分配 64B: " << ptr1 << std::endl;
    std::cout << "分配 128B: " << ptr2 << std::endl;
    std::cout << "分配 256B: " << ptr3 << std::endl;

    assert(ptr1 != nullptr);
    assert(ptr2 != nullptr);
    assert(ptr3 != nullptr);

    ConcurrentFree(ptr1, 64);
    ConcurrentFree(ptr2, 128);
    ConcurrentFree(ptr3, 256);

    std::cout << "基础分配测试通过 ✓" << std::endl;
}

// 测试SizeClass功能
void TestSizeClass()
{
    std::cout << "\n=== 测试大小分类 ===" << std::endl;

    assert(SizeClass::RoundUp(10) == 16);
    assert(SizeClass::RoundUp(130) == 144);
    assert(SizeClass::RoundUp(1500) == 1536);

    assert(SizeClass::Index(16) == 1);
    assert(SizeClass::Index(144) == 17);
    assert(SizeClass::Index(1536) == 73);

    std::cout << "大小分类测试通过 ✓" << std::endl;
}

// 测试FreeList功能
void TestFreeList()
{
    std::cout << "\n=== 测试自由链表 ===" << std::endl;

    FreeList list;

    char buffer[64];
    list.Push(buffer);
    void *popped = list.Pop();

    assert(popped == buffer);
    assert(list.Empty());

    // 使用 popped 避免警告
    (void)popped;

    std::cout << "自由链表测试通过 ✓" << std::endl;
}

// 测试ThreadCache单例
void TestThreadCacheSingleton()
{
    std::cout << "\n=== 测试ThreadCache单例 ===" << std::endl;

    // 这里我们无法直接测试单例，因为 ThreadCache 现在是前向声明
    // 我们通过 ConcurrentAlloc 来间接测试
    void *ptr = ConcurrentAlloc(64);
    assert(ptr != nullptr);
    ConcurrentFree(ptr, 64);

    std::cout << "ThreadCache功能测试通过 ✓" << std::endl;
}

// 测试边界情况
void TestEdgeCases()
{
    std::cout << "\n=== 测试边界情况 ===" << std::endl;

    void *tiny = ConcurrentAlloc(1);
    assert(tiny != nullptr);
    ConcurrentFree(tiny, 1);

    void *large = ConcurrentAlloc(256 * 1024);
    assert(large != nullptr);
    ConcurrentFree(large, 256 * 1024);

    std::cout << "边界情况测试通过 ✓" << std::endl;
}

// 简单多线程测试
void TestSimpleMultiThread()
{
    std::cout << "\n=== 测试简单多线程 ===" << std::endl;

    const int num_threads = 4;
    std::vector<std::thread> threads;

    for (int i = 0; i < num_threads; ++i)
    {
        threads.emplace_back([i]()
                             {
            std::vector<void*> pointers;
            for (int j = 0; j < 10; ++j) {
                size_t size = (j % 5 + 1) * 64;
                void* ptr = ConcurrentAlloc(size);
                pointers.push_back(ptr);
            }
            
            for (size_t j = 0; j < pointers.size(); ++j) {
                size_t size = (j % 5 + 1) * 64;
                ConcurrentFree(pointers[j], size);
            }
            
            std::cout << "线程 " << i << " 完成" << std::endl; });
    }

    for (auto &t : threads)
    {
        t.join();
    }

    std::cout << "简单多线程测试通过 ✓" << std::endl;
}

int main()
{
    std::cout << "开始测试并发内存池基本功能..." << std::endl;
    std::cout << "=========================================" << std::endl;

    try
    {
        TestSizeClass();
        TestFreeList();
        TestThreadCacheSingleton();
        TestBasicAllocation();
        TestEdgeCases();
        TestSimpleMultiThread();

        std::cout << "\n=========================================" << std::endl;
        std::cout << "所有基本功能测试通过！🎉" << std::endl;
    }
    catch (const std::exception &e)
    {
        std::cerr << "测试失败: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}